package com.ftg.learn.bus.config;

import com.ftg.learn.bus.service.impl.shiwuServiceImpl;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;


/**
 * @author Guo
 * @date 2022/3/12
 */
@Aspect
@Component
public class WebLogAspect {
    //    @Autowired
//    private IUserService iUserService;
    @Autowired
    private shiwuServiceImpl shiwuService;
    private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

//    public final static Map<String, String> MAP = new HashMap<String, String>(2);
//
//    static {
//        MAP.put("/bus/user/showAddRole", "查询角色全部");
//        MAP.put("/bus/user/showAddRoleXML", "查询全部人员，及其角色XML");
//    }

    //切入点描述 这个是controller包的切入点
    @Pointcut("execution(public * com.ftg.learn.bus.controller.*.*(..))")
    public void controllerLog() {
    }//签名，可以理解成这个切入点的一个名称

//    //切入点描述，这个是uiController包的切入点
//    @Pointcut("execution(public * com.stuPayment.uiController..*.*(..))")
//    public void uiControllerLog() {
//    }

    //    @Before("controllerLog() || uiControllerLog()") //在切入点的方法run之前要干的
//    @Before("controllerLog()") //在切入点的方法run之前要干的
//    public void logBeforeController(JoinPoint joinPoint) {
//        logger.info("在目标方法调用之前调用通知");
//    }

    @AfterReturning("controllerLog()")
    public void logBAfterReturningController() {
//        System.out.println("在目标方法成功执行之后调用通知");
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

        // 记录下请求内容
//        String userId = request.getParameter("userId");//获取请求参数 getParameter("参数名")
//        if (Objects.isNull(userId)) {
////            logger.error("userId null");
////            return;
//        }
        //获取请求地址
        String servletPath = request.getServletPath();
        shiwuService.shiwu(servletPath);
//        String gatPath = MAP.get(servletPath);//自定义判断操作路径不是MAP里的不处理
//        logger.info("登录成功");
//        if (Objects.nonNull(gatPath)) {
//            iUserService.addLog(Long.valueOf(userId), gatPath);
//            logger.info("登录后操作。。。。失败了");
//            int a = 1 / 0;
////            return false;
//        }
    }
}
